Fix Python mode error caused by incorrect indentation
authorkobarity <kobarity@gmail.com>
Sat, 16 Sep 2023 14:14:45 +0000 (23:14 +0900)
committerEli Zaretskii <eliz@gnu.org>
Sat, 8 Jun 2024 12:27:47 +0000 (15:27 +0300)
* lisp/progmodes/python.el (python-indent--calculate-indentation):
Guard against negative indentation.  (Bug #65870)

* test/lisp/progmodes/python-tests.el
(python-indent-badly-indented-block-end): New test.

lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index b5c00385ef3b299ca30766f22f573e17e5bdcd82..bb2bf1731b40047c12a5b8f8e91a31bc96157bc0 100644 (file)
@@ -1819,7 +1819,7 @@ possibilities can be narrowed to specific indentation points."
         (`(:after-block-end . ,start)
          ;; Subtract one indentation level.
          (goto-char start)
-         (- (current-indentation) python-indent-offset))
+         (max 0 (- (current-indentation) python-indent-offset)))
         (`(:at-dedenter-block-start . ,_)
          ;; List all possible indentation levels from opening blocks.
          (let ((opening-block-start-points
index b06547b10ff3f84b90f804aab68c158d28229c59..07fafde38cfac02cabde65431dd521c9061a1022 100644 (file)
@@ -2149,6 +2149,15 @@ def test_re(string):
    (python-tests-look-at "else:")
    (should (= (python-indent-calculate-indentation) 4))))
 
+(ert-deftest python-indent-badly-indented-block-end ()
+  "Test BUG 65870 regression."
+  (python-tests-with-temp-buffer
+   "
+return
+"
+   (goto-char (point-max))
+   (should (= (python-indent-calculate-indentation) 0))))
+
 \f
 ;;; Filling